package com.zx.platform.dao.impl;

import com.zx.platform.dao.RolesDao;
import com.zx.platform.dao.mapper.RoleMenusMapper;
import com.zx.platform.dao.mapper.RolesMapper;
import com.zx.platform.dao.model.*;
import com.zx.platform.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionTemplate;

import java.util.Date;
import java.util.List;

/**
 * add by wenjw 2015.5.13
 */
@Component("RolesDao")
public class RolesImpl implements RolesDao {

	@Autowired
	private RolesMapper mapper;

	@Autowired
	private RoleMenusMapper roleMenusMapper;

	@Autowired
	TransactionTemplate transactionTemplate;

	/**
	 * 新增
	 */
	public Integer add(Roles roles) throws DataAccessException {
		int column = mapper.insert(roles);
		if (column != 1) {
			throw new RuntimeException("");
		}
		return roles.getRoleId();
	}

	/**
	 * 修改
	 * 
	 * @param roles
	 * @return
	 * @throws DataAccessException
	 */
	public Integer update(Roles roles) throws DataAccessException {
		int column = mapper.updateByPrimaryKeySelective(roles);
		if (column != 1) {
			throw new RuntimeException("");
		}
		return roles.getRoleId();
	}

	/**
	 * 删除
	 */
	public void delete(Roles roles) throws DataAccessException {
		int column = mapper.deleteByPrimaryKey(roles.getRoleId());
		if (column != 1) {
			throw new RuntimeException("");
		}
	}

	/**
	 * 分页查找
	 */
	public List<Roles> getPageByExample(int pageSize, int pageBegin,
			RolesExample example) throws DataAccessException {
		return mapper.getPageByModel(pageSize, pageBegin, example);
	}

	/**
	 * 查找条数
	 * 
	 * @param example
	 * @return
	 * @throws DataAccessException
	 */
	public int countByExample(RolesExample example) throws DataAccessException {
		return mapper.countByExample(example);
	}

	/**
	 * 
	 */
	public List<Roles> getListByExample(RolesExample example)
			throws DataAccessException {
		return mapper.selectByExample(example);
	}

	public Roles selectByPrimaryKey(Integer key) throws DataAccessException {
		return mapper.selectByPrimaryKey(key);
	}

	/**
	 * 设置角色菜单关联 是先删除再新增 menuIds通过分号隔开 事务保持一致 不知这样写事务对不对
	 */
	// 如果有事务,那么加入事务,没有的话新建一个(不写的情况下)
    @Transactional(propagation=Propagation.REQUIRED)
	public Integer setRoleMenus(RoleMenusTemp temp)
			throws DataAccessException {
		Integer roleId = temp.getRoleId();
		String menuIds = temp.getMenuIds();
		// 先删除关联关系
		RoleMenusExample roleMenusExample = new RoleMenusExample();
		com.zx.platform.dao.model.RoleMenusExample.Criteria criteria = roleMenusExample.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		roleMenusMapper.deleteByExample(roleMenusExample);
		// 再增加
		if (StrUtils.isNotEmpty(menuIds)) {
			String[] menuIdStrs = menuIds.split(";");
			for (int i = 0; i < menuIdStrs.length; i++) {
				String menuIdStr = menuIdStrs[i];
				if (StrUtils.isNotEmpty(menuIdStr)) {
					Integer menuId = 0;
					try {
						menuId = Integer.valueOf(menuIdStr);
						RoleMenus record = new RoleMenus();
						record.setRoleId(roleId);
						record.setMenuId(menuId);
						record.setCreateDate(new Date());
						roleMenusMapper.insert(record);
					} catch (Exception e) {
						// 转换异常则不处理
					}
				}
			}
		}
		return roleId;
	}
}
